package com.toraeve.asktora.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.toraeve.asktora.domain.entity.ToraUser;
import com.toraeve.asktora.domain.service.UserService;
import com.toraeve.asktora.security.entity.ToraUserDetailsAdapter;

@Service
public class ToraUserService implements UserDetailsService {

	private UserService userService;

	@Required
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		// we use email as spring-security's username
		String email = username;
		ToraUser toraUser = userService.getUserByEmail(email);
		ToraUserDetailsAdapter adapter = new ToraUserDetailsAdapter(toraUser);
		return adapter;
	}

}
